// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.standard.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.common.ImportResp;
import com.liuxinlong.common.UploadService;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.Standard;
import com.liuxinlong.modules.standard.controller.request.ImportDesignStandardReq;
import com.liuxinlong.modules.standard.controller.request.QueryDesignStandardReq;
import com.liuxinlong.modules.standard.controller.request.UpdateDesignStandardReq;
import com.liuxinlong.modules.standard.service.StandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 标准控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年1月21日
 */
@RestController
@RequestMapping("/standard")
public class StandardController {

    @Autowired
    private StandardService standardService;

    @Autowired
    private UploadService uploadService;

    /**
     * 分页查询标准信息
     *
     * @param req 请求信息
     * @return 标准信息列表
     */
    @PostMapping("page")
//    @LogAnnotation(value = "分页查询", business = LogBusinessConstants.DESIGN_STANDARD,operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pageStandardInfo(@RequestBody QueryDesignStandardReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("name", req.getName());
        queryMap.put("standardSn", req.getStandardSn());
        queryMap.put("securityLevel", req.getSecurityLevel());
        queryMap.put("status", req.getStatus());
        queryMap.put("classification", req.getClassification());
        queryMap.put("hierarchy", req.getHierarchy());
        List<Map<String, Object>>  dataList = standardService.pageStandardInfo(queryMap);
        int totalNum = standardService.getStandardCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询标准信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 新增标准信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增标准信息", business = LogBusinessConstants.DESIGN_STANDARD,operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addStandardInfo(@RequestBody UpdateDesignStandardReq req) {
        Standard design = new Standard();
        design.setName(req.getName());
        design.setStandardSn(req.getStandardSn());
        design.setHierarchy(req.getHierarchy());
        design.setStandardType(req.getStandardType());
        design.setReviewDepartment(req.getReviewDepartment());
        design.setClassification(req.getClassification());
        design.setSubClassification(req.getSubClassification());
        design.setOldName(req.getOldName());
        design.setMandatory(req.getMandatory());
        design.setSecurityLevel(req.getSecurityLevel());
        design.setVersion(req.getVersion());
        design.setTrialStartTime(req.getTrialStartTime());
        design.setTrialEndTime(req.getTrialEndTime());
        design.setReviewTime(req.getReviewTime());
        design.setApplicability(req.getApplicability());
        design.setReference(req.getReference());
        design.setReferenceAttach(req.getReferenceAttach());
        design.setGroupId(req.getGroupId());
        design.setMasterFile(req.getMasterFile());
        design.setMasterPdfFile(req.getMasterPdfFile());
        design.setAttach(req.getAttach());
        design.setRemark(req.getRemark());
        standardService.addStandardInfo(design,req.getContent());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增标准信息成功");
        return resp;

    }

    /**
     * 修改标准信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "更新标准信息", business = LogBusinessConstants.DESIGN_STANDARD,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateStandardInfo(@RequestBody UpdateDesignStandardReq req) {
        Standard design = new Standard();
        design.setId(req.getId());
        design.setName(req.getName());
        design.setStandardSn(req.getStandardSn());
        design.setHierarchy(req.getHierarchy());
        design.setStandardType(req.getStandardType());
        design.setDepartment(req.getDepartment());
        design.setReviewDepartment(req.getReviewDepartment());
        design.setClassification(req.getClassification());
        design.setSubClassification(req.getSubClassification());
        design.setOldName(req.getOldName());
        design.setMandatory(req.getMandatory());
        design.setSecurityLevel(req.getSecurityLevel());
        design.setVersion(req.getVersion());
        design.setTrialStartTime(req.getTrialStartTime());
        design.setTrialEndTime(req.getTrialEndTime());
        design.setReviewTime(req.getReviewTime());
        design.setApplicability(req.getApplicability());
        design.setReference(req.getReference());
        design.setReferenceAttach(req.getReferenceAttach());
        design.setGroupId(req.getGroupId());
        design.setMasterFile(req.getMasterFile());
        design.setMasterPdfFile(req.getMasterPdfFile());
        design.setAttach(req.getAttach());
        design.setRemark(req.getRemark());
        standardService.updateStandardInfo(design,req.getContent());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改标准信息成功");
        return resp;
    }

    /**
     * 删除标准信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除标准信息", business = LogBusinessConstants.DESIGN_STANDARD,operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteStandardInfo(@RequestBody BaseDeleteReq req) {
        standardService.deleteStandardInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除标准信息成功");
        return resp;
    }

    /**
     * 废止标准
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("abolish")
    @LogAnnotation(value = "废止标准", business = LogBusinessConstants.DESIGN_STANDARD,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp abolishStandard(@RequestBody BaseDeleteReq req) {
        standardService.abolishStandard(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("废止标准成功");
        return resp;
    }

    /**
     * 导出标准信息
     *
     * @param req 请求信息
     * @return 导出数据
     */
    @PostMapping("export")
    @LogAnnotation(value = "导出标准信息", business = LogBusinessConstants.DESIGN_STANDARD,operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportStandardInfo(@RequestBody BaseDeleteReq req, HttpServletResponse response) {
        standardService.exportStandardInfo(req.getId(), response);
    }

    /**
     * 导入数据预处理
     *
     * @param multipartRequest 文件
     * @return 数据
     */
    @PostMapping("import")
    @LogAnnotation(value = "导入标准数据", business = LogBusinessConstants.DESIGN_STANDARD,operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp importDetails(MultipartRequest multipartRequest) {
        Map<String, Object> map = uploadService.uploadFileLocal(multipartRequest);
        ImportResp resp = standardService.importStandards((String) map.get("fileRealPath"));
        return resp;
    }

    /**
     * 导入标准信息
     *
     * @param req 请求体
     * @return 返回导入失败数据
     */
    @PostMapping("batch-add")
    @LogAnnotation(value = "导入标准数据", business = LogBusinessConstants.DESIGN_STANDARD,operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp batchAddStandard(@RequestBody ImportDesignStandardReq req) {
        return standardService.batchAddStandard(req.getDataList());
    }

    /**
     * 标准锁定
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("lock")
    @LogAnnotation(value = "标准锁定", business = LogBusinessConstants.DESIGN_STANDARD,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp lockDesignStandard(@RequestBody BaseDeleteReq req) {
        standardService.lockDesignStandard(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("标准锁定成功");
        return resp;
    }

    /**
     * 标准流程发起
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("process")
    @LogAnnotation(value = "标准流程发起", business = LogBusinessConstants.DESIGN_STANDARD,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp processStandard(@RequestBody BaseDeleteReq req) {
        standardService.processStandard(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("标准流程发起成功");
        return resp;
    }

    /**
     * 标准锁定
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("unlock")
    @LogAnnotation(value = "标准解锁", business = LogBusinessConstants.DESIGN_STANDARD,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp unlockDesignStandard(@RequestBody BaseDeleteReq req) {
        standardService.unlockDesignStandard(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("标准解锁成功");
        return resp;
    }

    /**
     * 查询标准草稿信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("loadDraft")
    @LogAnnotation(value = "查询标准草稿信息", business = LogBusinessConstants.DESIGN_STANDARD,operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp loadDraft(@RequestBody BaseDeleteReq req) {
        Map<String, Object> draft = standardService.loadDraft(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("标准草稿信息查询成功");
        resp.setDataMap(draft);
        return resp;
    }

    /**
     * 查询标准版本信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("version")
    @LogAnnotation(value = "查询标准版本信息", business = LogBusinessConstants.DESIGN_STANDARD,operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp queryStandardVersion(@RequestBody BaseDeleteReq req) {
        List<Map<String, Object>> dataList = standardService.queryStandardVersion(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("标准版本信息查询成功");
        resp.setDataList(dataList);
        return resp;
    }

    /**
     * 查询标准文件信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("file")
    @LogAnnotation(value = "查询标准文件信息", business = LogBusinessConstants.DESIGN_STANDARD,operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp queryStandardFile(@RequestBody BaseDeleteReq req) {
        List<Map<String, Object>> dataList = standardService.queryStandardFile(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("标准文件信息查询成功");
        resp.setDataList(dataList);
        return resp;
    }
}
